/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import helper.Utilities;
import java.io.IOException;

/**
 *
 * @author Hallucinogen
 */
public class PeerThread extends Thread {

    public PeerThread(Peer peer) {
        m_Peer      = peer;
        m_Running   = false;
    }

    @Override
    public void run() {
        m_Running = true;
        while (m_Running) {
            if (System.currentTimeMillis() - m_Peer.getLastRequest() < Utilities.PEER_REQUEST_INTERVAL) {
                try {
                    sleep(Utilities.PEER_REQUEST_INTERVAL - (System.currentTimeMillis() - m_Peer.getLastRequest()));
                } catch (InterruptedException ex){
                    // do nothing
                }
            }

            try {
                m_Peer.setResult(m_Peer.requestFileToTracker(m_Peer.getData()));
            } catch (IOException ex){
                // do nothing juga
            }
        }
    }

    public Peer getPeer(){
        return m_Peer;
    }

    public boolean isRunning(){
        return m_Running;
    }

    public void setRunning(boolean running){
        m_Running = running;
    }

    private Peer m_Peer;
    private boolean m_Running;
}
